<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var groupAnagrams = function (strs) {
        const map = new Map()
        let result = []
        for (let i = 0; i < strs.length; i++) {
          let s = strs[i].split('').sort().join('')
          if (map.has(s)) {
            let value = map.get(s)
            map.set(s, [...value, strs[i]])
          } else {
            map.set(s, [strs[i]])
          }
        }
        for (let [key, value] of map) {
          result.push(value)
        }
        return result
      }
      console.log(groupAnagrams(['eat', 'tea', 'tan', 'ate', 'nat', 'bat']))
    </script>
  </body>
</html>
